<?php

namespace app\controller;

use support\Request;
use support\Response;

class UserController
{
	public function hello(Request $request)
	{
		$default_name = 'webman';
		// 从get请求里获得name参数，如果没有传递name参数则返回$default_name
		$name = $request->get('name', $default_name);
		// 向浏览器返回字符串
		return response('hello ' . $name);
	} 
	
	public function hello1(Request $request)
	{
		$default_name = 'webman';
		$name = $request->get('name', $default_name);
		return json([
				'code' => 0,
				'msg' => 'ok',
				'data' => $name
		]);
	}
	
	public function hello2(Request $request)
	{
		$default_name = 'webman';
		$name = $request->get('name', $default_name);
		return view('user/hello', ['name' => $name]);
	}
	
	public function hello4(Request $request)
	{
		return response('hello webman');
	}
	
	public function hello3(Request $request)
	{
		$request->get();
		
		$request->get('name');
		
		$request->get('name', 'tom');
		
		$request->post();
		
		$request->post('name', 'tom');
		
		$post = $request->rawBody();
		
		$request->header();
		
		$request->header('host');
		
		$request->header('host', 'localhost');
		
		$request->cookie(); 
		
		$request->cookie('name', 'tom');
		
		
		$request->all();
		
		$request->input('name', "tom");
		
		
		// 获取 username 和 password 组成的数组，如果对应的key没有则忽略
		$only = $request->only(['username', 'password']);
		// 获得除了avatar 和 age 以外的所有输入
		$except = $request->except(['avatar', 'age']);
		
		
		$request->file();
	}
	
	
	public function create(string $name, int $age = 18): Response
	{
		return json(['name' => $name, 'age' => $age]);
	}
	
	public function create1(Request $request): Response
	{
		$name = $request->input('name');
		$age = (int)$request->input('age', 18);
		return json(['name' => $name, 'age' => $age]);
	}

	
	public function files(Request $request)
	{
		foreach ($request->file() as $key => $spl_file) {
			var_export($spl_file->isValid()); // 文件是否有效，例如ture|false
			var_export($spl_file->getUploadExtension()); // 上传文件后缀名，例如'jpg'
			var_export($spl_file->getUploadMimeType()); // 上传文件mine类型，例如 'image/jpeg'
			var_export($spl_file->getUploadErrorCode()); // 获取上传错误码，例如 UPLOAD_ERR_NO_TMP_DIR UPLOAD_ERR_NO_FILE UPLOAD_ERR_CANT_WRITE
			var_export($spl_file->getUploadName()); // 上传文件名，例如 'my-test.jpg'
			var_export($spl_file->getSize()); // 获得文件大小，例如 13364，单位字节
			var_export($spl_file->getPath()); // 获得上传的目录，例如 '/tmp'
			var_export($spl_file->getRealPath()); // 获得临时文件路径，例如 `/tmp/workerman.upload.SRliMu`
		}
		return response('ok');
		
		
		$request->file('avatar');
		
		$file = $request->file('avatar');
		if ($file && $file->isValid()) {
			$file->move(public_path().'/files/myfile.'.$file->getUploadExtension());
			return json(['code' => 0, 'msg' => 'upload success']);
		}
		return json(['code' => 1, 'msg' => 'file not found']);
		
		
		$request->host();
		
		$request->host(true);
		
		$request->method();
		
		$request->uri();
		
		$request->path();
		
		
		$request->queryString();
		
		
		$request->url();
		
		$request->fullUrl();
		
		
		echo $request->header('x-forwarded-proto'); // 输出 http 或 https
		
		$request->protocolVersion();
		
		$request->sessionId();
		
		
		$request->getRemoteIp();
		
		
		$request->getRemotePort();
		
		
		$request->getRealIp($safe_mode=true);
		
		
		$request->getLocalIp();
		
		
		$request->getLocalPort();
		
		
		$request->isAjax();
		
		
		$request->isPjax();
		
		
		$request->expectsJson();
		
		
		$request->acceptJson();
		
		$aa = $request->plugin;
		
		
		$aa =$request->app;
		
		$aa = $request->action;
		
		
		$request->get(); // 假设得到 ['name' => 'tom', 'age' => 18]
		$request->setGet(['name' => 'tom']);
		$request->get(); // 最终得到 ['name' => 'tom']
		
		
		$post = $request->post();
		foreach ($post as $key => $value) {
			$post[$key] = htmlspecialchars($value);
		}
		$request->setPost($post);
		$request->post(); // 得到过滤后的post参数
		
		
		
		$request->setHeader(['host' => 'example.com']);
		$request->header('host'); // 输出 example.com
	}
	
	function response($body = '', $status = 200, $headers = array())
	{
		return new Response($status, $headers, $body);
	}
}
